package com.gviiii.baiduai.config;

import com.google.common.base.Strings;

import java.util.*;

public class ConfigHandler {

    private static final String AK = "ak";

    private static final String SK = "sk";

    private static final String APPID = "appid";

    private static final String API = "api";

    private static final String AI = "ai";

    private static final String SOURCE = "source";

    private static final String SINK = "sink";

    private static final String NAME = "name";

    private static final String comma = ",";

    private static final String dot = ".";


    protected static Map<String, String> getTokenConfigMap(Map<String, String> configMap) {
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put(AK, configMap.get(AK));
        tokenMap.put(SK, configMap.get(SK));
        if (configMap.containsKey(API)) {
            tokenMap.put(API, configMap.get(API));
        }
        if (configMap.containsKey(APPID)) {
            tokenMap.put(APPID, configMap.get(APPID));
        }
        return tokenMap;
    }

    protected static Map<String, ServiceConfig> getAiConfigMap(Map<String, String> configMap) {
        Map<String, ServiceConfig> serviceMap = new HashMap<>();
        String ai = configMap.get(AI);
        if (!Strings.isNullOrEmpty(ai)) {
            String aiName = configMap.get(ai + dot + NAME);
            if (!Strings.isNullOrEmpty(aiName)) {
                Set<String> AiModelNameSet = splitCommStrings(aiName);
                AiModelNameSet.forEach(model -> {
                    ServiceConfig serviceConfig = new ServiceConfig();
                    serviceConfig.setServiceName(model);
                    generateAiSourceConfig(configMap, serviceConfig, model, ai);
                    generateAiApiConfig(configMap, serviceConfig, model, ai);
                    generateAiSinkConfig(configMap, serviceConfig, model);
                    serviceMap.put(model, serviceConfig);
                });
            }
        }
        return serviceMap;
    }

    protected static Map<String, ServiceConfig> getSdkConfigMap(Map<String, String> configMap) {
        Map<String, String> tokenConfigMap = getTokenConfigMap(configMap);
        Map<String, ServiceConfig> aiConfigMap = getAiConfigMap(configMap);
        Iterator<Map.Entry<String, ServiceConfig>> iterator = aiConfigMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, ServiceConfig> entry = iterator.next();
            ServiceConfig serviceConfig = entry.getValue();
            serviceConfig.setAppid(tokenConfigMap.get("appid"));
            serviceConfig.setAk(tokenConfigMap.get("ak"));
            serviceConfig.setSk(tokenConfigMap.get("sk"));
            String key = entry.getKey();
            aiConfigMap.remove(key);
            aiConfigMap.put(key, serviceConfig);
        }
        return aiConfigMap;
    }

    private static void generateAiSourceConfig(Map<String, String> configMap, ServiceConfig serviceConfig, String model, String serviceName) {
        if (configMap.containsKey(model + dot + SOURCE)) {
            serviceConfig.setSource(configMap.get(model + dot + SOURCE));
        } else {
            serviceConfig.setSource(configMap.get(serviceName + dot + SOURCE));
        }
    }

    private static void generateAiApiConfig(Map<String, String> configMap, ServiceConfig serviceConfig, String model, String serviceName) {
        if (configMap.containsKey(model + dot + API)) {
            serviceConfig.setApi(configMap.get(model + dot + API));
        } else {
            serviceConfig.setApi(configMap.get(serviceName + dot + API));
        }
    }

    private static void generateAiSinkConfig(Map<String, String> configMap, ServiceConfig serviceConfig, String model) {
        Map<String, String> map = new HashMap<>();
        configMap.keySet().forEach(s -> {
            String[] dotStrings = splitDotStrings(s);
            if (dotStrings[0].equals(model) && dotStrings[dotStrings.length - 1].equals(SINK)) {
                map.put(dotStrings[1], configMap.get(s));
            }
        });
        serviceConfig.setResultMap(map);
    }

    @Deprecated
    public static Set<String> getAiServiceName(Map<String, String> configMap) {
        String s = configMap.get(AI);
        return splitCommStrings(s);
    }

    @Deprecated
    public static Set<String> getAiModelName(Map<String, String> configMap) {
        Set<String> modelNameSet = new HashSet<>();
        getAiServiceName(configMap).forEach(x -> {
            String s = configMap.get(createKey(AI, x, NAME));
            Set<String> strings = splitCommStrings(s);
            modelNameSet.addAll(strings);
        });
        return modelNameSet;
    }

    private static Set<String> splitCommStrings(String s) {
        Set<String> nameSet = new HashSet<>();
        if (s.contains(comma)) {
            String[] split = s.split(comma);
            for (int i = 0; i < split.length; i++) {
                nameSet.add(split[i]);
            }
        }
        nameSet.add(s);
        return nameSet;
    }

    @Deprecated
    public static String createKey(String... args) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < args.length; i++) {
            if (args.length - 1 == i) {
                sb.append(args[i]);
                break;
            }
            sb.append(args[i]).append(dot);
        }
        return sb.toString();
    }

    @Deprecated
    public static boolean containChar(String srcStr, String chars) {
        return srcStr.contains(chars);
    }

    public static Map<String, String> initConfig(Properties prop) {
        HashMap<String, String> configMap = new HashMap<>();
        prop.entrySet().forEach(x -> configMap.put(String.valueOf(x.getKey()), (String) x.getValue()));
        return configMap;
    }

    private static String[] splitDotStrings(String s) {
        String[] split = s.split("\\" + dot);
        return split;
    }
}
